package com.dmxh.service.impl;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import com.dmxh.mapper.TbSeckillGoodsMapper;
import com.dmxh.pojo.TbSeckillGoods;
import com.dmxh.pojo.TbSeckillGoodsExample;
import com.dmxh.pojo.TbSeckillOrder;
import com.dmxh.service.SeckillGoodsService;
import com.dmxh.vo.SeckillGoodsVo;
import com.dmxh.vo.SeckillOrderVo;
import com.dmxh.vo.UserVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

@Service
public class SeckillGoodsServiceImpl implements SeckillGoodsService {

    @Autowired
    private TbSeckillGoodsMapper seckillGoodsMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Override
    public Map<String, Object> querySeckillGoods() {
        List<TbSeckillGoods> seckillGoodsList = this.seckillGoodsMapper.selectByExample(null);
        List<SeckillGoodsVo> collect = seckillGoodsList.stream().map(seckillvo -> {
            SeckillGoodsVo seckillGoodsVo = new SeckillGoodsVo();
            BeanUtils.copyProperties(seckillvo, seckillGoodsVo);
            return seckillGoodsVo;
        }).collect(Collectors.toList());
        Map<String, Object> map = new HashMap<>();
        map.put("seckillGoods", collect);
        return map;
    }

    @Override
    public void querySeckillBeforeTheEndGoods() {
        TbSeckillGoodsExample example = new TbSeckillGoodsExample();
        Date now = new Date();
        /*现在  代码解读:开始时间小于当前时间    结束时间大于当前时间
        详解:LessThanOrEqualTo=小于等于    GreaterThanOrEqualTo=大于等于*/
        example.createCriteria()
                .andStartTimeLessThanOrEqualTo(now)
                .andEndTimeGreaterThanOrEqualTo(now);
        List<TbSeckillGoods> secKillGoods = seckillGoodsMapper.selectByExample(example);
        if (Objects.isNull(secKillGoods)) {
            throw new RuntimeException("数据库查询数据为空->无法添加至缓存");
        }
        secKillGoods.stream().map(secKillGoodsListVo -> {
            SeckillGoodsVo secKillGoodsVo = new SeckillGoodsVo();
            BeanUtils.copyProperties(secKillGoodsListVo, secKillGoodsVo);
            return secKillGoodsVo;
        }).forEach(secKillVo -> redisTemplate.boundHashOps("secKillGoodsList").put(secKillVo.getGoodsId(), secKillVo));
    }

    @Override
    public SeckillGoodsVo getRedisData(Long secKillGoodsId) {
        SeckillGoodsVo seckillGoodsVo = (SeckillGoodsVo) redisTemplate.boundHashOps("secKillGoodsList").get(secKillGoodsId);
        if (Objects.isNull(seckillGoodsVo)) {
            throw new RuntimeException("缓存里没有秒杀数据哦");
        }
        return seckillGoodsVo;
    }

    @Override
    public List<SeckillGoodsVo> getRedisSecKillData() {
        List<SeckillGoodsVo> secKillGoodsList = redisTemplate.boundHashOps("secKillGoodsList").values();//只获取value值
        return secKillGoodsList;
    }

    @SentinelResource("common")
    @Override
    public void common() {
        System.out.println("common方法执行");
    }

    @Override
    public SeckillGoodsVo getSeckillGoodsByGoodsId(Long goodsId) {
        TbSeckillGoodsExample example = new TbSeckillGoodsExample();
        example.createCriteria().andGoodsIdEqualTo(goodsId);
        List<TbSeckillGoods> tbSeckillGoods = seckillGoodsMapper.selectByExample(example);
        if (Objects.isNull(tbSeckillGoods)) {
            throw new RuntimeException("根据goodsId查询数据库,数据为空");
        }
        TbSeckillGoods tbSeckillGoods1 = tbSeckillGoods.get(0);
        SeckillGoodsVo seckillGoodsVo = new SeckillGoodsVo();
        BeanUtils.copyProperties(tbSeckillGoods1, seckillGoodsVo);
        return seckillGoodsVo;
    }
}
